package cn.com.java.controller;

import java.util.List;

import cn.com.java.core.HttpRes;
import cn.com.java.model.ProductType;
import cn.com.java.model.SysRole;
import cn.com.java.service.ProductTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("ProductType")
@RequiredArgsConstructor
public class ProductTypeController {

    
    private final ProductTypeService productTypeService;

    @PostMapping("listByPage")
    public HttpRes listByPage(@RequestBody ProductType productType) {
        Page<ProductType> page = new Page<>(productType.getPageIndex(), productType.getPageSize());

        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        if (productType.getTypeName() != null && !productType.getTypeName().isEmpty()) {
            productTypeQueryWrapper.like("type_name", productType.getTypeName() );
        }
        return HttpRes.OK("查询成功", productTypeService.page(page, productTypeQueryWrapper));
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<ProductType> listAll() {
        return productTypeService.list();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public ProductType getById(Long id) {
        return productTypeService.getById(id);
    }    
     
    /**
     * 新增，忽略null字段
     *
     * @param productType 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public HttpRes insert(@RequestBody ProductType productType) {
        return HttpRes.OK(productTypeService.save(productType));
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param productType 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public HttpRes update(@RequestBody ProductType productType) {
        return HttpRes.OK(productTypeService.updateById(productType));
    }
    
    /**
     * 删除记录
     *
     * @param productType 待删除的记录
     * @return 返回影响行数
     */
    @RequestMapping("delete")
    public HttpRes delete(@RequestBody ProductType productType) {
        return HttpRes.OK(productTypeService.removeById(productType));
    }
    
}